package com.lostandfound.dao.impl;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.lostandfound.dao.ThanksDao;
import com.lostandfound.entity.ThanksLetter;

@Repository("thanksDao")
public class ThanksDaoImpl implements ThanksDao {
	
	private Log logger = LogFactory.getLog(this.getClass());
	
	@Autowired
	private SessionFactory sessionFactory;

	@Override
	public Serializable save(ThanksLetter obj) {
		logger.info("发表了一个感谢信");
		return sessionFactory.getCurrentSession().save(obj);
	}

	@Override
	public void update(ThanksLetter obj) {}

	@Override
	public void delete(ThanksLetter obj) {}

	@Override
	public ThanksLetter selectByPrimary(Long id) {
		return (ThanksLetter)sessionFactory.getCurrentSession().get(ThanksLetter.class, id);
	}

	@Override
	public List<?> listThanksByPage(Map<String, Object> pagemap) {
		
		Session session = sessionFactory.getCurrentSession();
		String hql = "from ThanksLetter t where t.lstatus = '0' ";
		if(pagemap != null){
			if(pagemap.get("userCode") != null && !((String)pagemap.get("userCode")).equals("")){
				hql = hql + "and t.user.userCode like '"
						+(String)pagemap.get("userCode")+ "%' ";
			}
			if(pagemap.get("pname") != null && !((String)pagemap.get("pname")).equals("")){
				hql = hql + "and t.plate.pname like '"
						+ (String)pagemap.get("pname") +"%' ";
			}
		}
		int page = Integer.parseInt((String)pagemap.get("page"));
		int size = Integer.parseInt((String)pagemap.get("size"));
		Query query = session.createQuery(hql)
				.setFirstResult((page-1)*size)
				.setMaxResults(size);
		List<?> list = query.list();

		return list;
	}

	@Override
	public int Count(Map<String, Object> pagemap) {
		Session session = sessionFactory.getCurrentSession();
		String hql = "select count(*) from ThanksLetter t where t.lstatus = '0' ";
		if(pagemap != null){
			if(pagemap.get("userCode") != null && !((String)pagemap.get("userCode")).equals("")){
				hql = hql + "and t.user.userCode like '"
						+(String)pagemap.get("userCode")+ "%' ";
			}
			if(pagemap.get("pname") != null && !((String)pagemap.get("pname")).equals("")){
				hql = hql + "and t.plate.pname like '"
						+ (String)pagemap.get("pname") +"%' ";
			}
		}
		Query query = session.createQuery(hql);
		return ((Number) query.iterate().next())
	  	         .intValue();
	}

	@Override
	public void deleteThanksByBranch(Long[] thanks) {
		String hql = "";
		for(int i=0;i<thanks.length;i++) {
            if(i==0) {
                hql = "t.lid="+thanks[i];
            } else {
                hql =hql + " or t.lid="+thanks[i];
            }
        }
		if(!hql.equals("")){
	        Session session= sessionFactory.getCurrentSession();
	        Query q= session.createQuery("update ThanksLetter t set t.lstatus = '1' where " + hql);
	        q.executeUpdate();
			logger.info("批量删除感谢信" + thanks);
		}
	}

	@Override
	public void deleteThanksByUser(Long uid) {
		Session session = sessionFactory.getCurrentSession();
		String hql = "update ThanksLetter t set t.tstatus = '1' where t.user.id = ? ";
		session.createQuery(hql).setParameter(0, uid)
				.executeUpdate();
	}

	@Override
	public void deleteThanksByTopic(Long tid) {
		Session session = sessionFactory.getCurrentSession();
		String hql = "update ThanksLetter t set t.tstatus = '1' where t.topic.tid = ? ";
		session.createQuery(hql).setParameter(0, tid)
				.executeUpdate();
	}

}
